package _021_11_17

import (
	"strings"
)

func checkIsContain(str string, t string) bool {

	for j := 0; j < len(t); j++ {
		if !strings.Contains(str, string(t[j])) {
			return false
		}
	}
	return true
}

func _minWindow(s string, t string) string {
	ret := make([]string, 0)
	before := 0
	after := 1

	for before < len(s) && after < len(s) {
		if checkIsContain(s[before:after+1], t) {
			ret = append(ret, s[before:after+1])
			before++
			continue
		}
		after++
	}

	smallStr := ""
	for i := 0; i < len(ret); i++ {
		if len(smallStr) > len(ret[i]) {
			smallStr = ret[i]
		}
	}
	for i := 0; i < len(ret); i++ {
		if len(ret[i]) == len(smallStr) {
			if ret[i] < smallStr {
				smallStr = ret[i]
			}
		}
	}
	return smallStr
}

func checkIsSuit(mT map[uint8]uint8) bool {
	for _, v := range mT {
		if v > 0 {
			return false
		}
	}
	return true
}

func minWindow(s string, t string) string {
	ret := make([]string, 0)
	mT := make(map[uint8]uint8)

	for i := 0; i < len(t); i++ {
		mT[t[i]]++
	}

	before, after := 0, 0

	for after < len(s) {
		if checkIsSuit(mT) {
			ret = append(ret, s[after:before+1])

			if _, ok := mT[s[before]]; ok {
				mT[s[before]]++
			}

			before++
			continue
		}
		if _, ok := mT[s[after]]; ok {
			mT[s[after]]--
		}
		after++
	}

	if len(ret) == 0 {
		return ""
	}

	maxStr := ret[0]
	for i := 1; i < len(ret); i++ {

		if maxStr < ret[i] {
			maxStr = ret[i]
		}

	}

	return maxStr
}
